package org.nursing.controller;

import org.nursing.entity.User;
import org.nursing.service.UserService;
import org.nursing.util.BaseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

//在使用 @RestController 注解标记的类中，每个方法的返回值都会以 JSON 或 XML 的形式直接写入 HTTP 响应体中，
// 相当于在每个方法上都添加了 @ResponseBody 注解。
@RestController
public class UserController {
    //相当于声明了对象
    @Autowired
    private UserService userService;
    //用于接口处理请求方法的POST类型
    @PostMapping("/login")
    private BaseResult<User> get(@RequestParam String loginName, String password, String tip, HttpSession session){
        User user = userService.login(loginName,password);
        //将user保存在session中
        if (user == null){
            return BaseResult.failed(null,"用户名或密码错误！");
        }else {
            session.setAttribute("user", user);
            return BaseResult.ok(user, "登录成功！");
        }
    }
    //注册
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public BaseResult<Object> register(User user) {
        int result = userService.register(user);
        if (result > 0){
            return BaseResult.ok(null,"注册成功！");
        }else {
            return BaseResult.ok(user, "注册失败！");
        }
    }
}
